#include "HY1C_out.h"
#include <stdio.h>
#include <string.h>
#include <libgen.h>
#include <unistd.h>
#include <netcdf.h>
#include <string.h>

#include "l12_parms.h"
#include "hdf_utils.h"
#include "filehandle.h"
#include "l1_mos_hdf.h"
#include "l1_octs_hdf.h"
#include "l1_osmi_hdf.h"
#include "l1_modis_hdf.h"
#include "l1_ocm_hdf.h"
#include "l1_ocm2_hdf.h"
#include "l1_meris_N1.h"
#include "l1_viirs_h5.h"

#define EOSMETALEN 32768

int32_t sensorName2ID(char *name) {
    int32_t sensorID = -1;
    int i;

    for (i = 0; i < SENSOR_NUM; i++)
        if (strcmp(name, sensorName[i]) == 0) {
            sensorID = i;
            break;
        }

    return (sensorID);
}

int32_t getFormat(filehandle *file) {
    int32_t fmt = -1;
    int32_t ocean_subset = -1;
    int32_t sd_id;
    char eosmeta[EOSMETALEN] = "";
    char tempstr[32] = "";
    int32_t chk_viirs(char *, filehandle *);
    int ncid, status;

    file->mode = READ;

    /* Does the file exist? */
    if (access(file->name, F_OK) || access(file->name, R_OK)) {
        HY1C_out("-E- %s: Input file '%s' does not exist or cannot open.\n",
                __FILE__, file->name);
        return (fmt);
    }

    /* Is it HDF? */

    sd_id = SDstart(file->name, DFACC_RDONLY);
    if (sd_id != FAIL) {

        /* File is HDF. Is it one of ours? */

        char title[255];
        char sensor[80];
        if (SDreadattr(sd_id, SDfindattr(sd_id, "Title"), (VOIDP) title) == 0) {

            if (strstr(title, "Level-3 Binned Data") != NULL) {
                if (SDreadattr(sd_id, SDfindattr(sd_id, "Sensor Name"), (VOIDP) sensor) == 0) {

		    if (file->ocrvc_opt > 0) strcpy(sensor,"OCRVC");

                    // kludge for VIIRS EDR L3
                    if (strcmp(sensor,"VIIRS") == 0) strcpy(sensor,"VIIRSN");

                    if ((file->sensorID = sensorName2ID(sensor)) != -1) {
                        fmt = FMT_L3BIN;
                        file->format = fmt;
                        if (want_verbose)
                            HY1C_out("Input file %s is %s.\n", file->name, title);
                    } else {
                        fHY1C_out(stderr,
                                "-E- %s Line %d: Unknown sensor name in Level-3 file %s\n",
                                __FILE__, __LINE__, file->name);
                        return (fmt);
                    }
                } else {
                    fHY1C_out(stderr,
                            "-E- %s Line %d: No sensor name attribute in Level-3 file %s\n",
                            __FILE__, __LINE__, file->name);
                    return (fmt);
                }
            } else if (strstr(title, "Level-2 Data") != NULL) {
                if (SDreadattr(sd_id, SDfindattr(sd_id, "Sensor Name"), (VOIDP) sensor) == 0) {
                    if ((file->sensorID = sensorName2ID(sensor)) != -1) {
                        fmt = FMT_MSL12L2;
                        file->format = fmt;
                        if (want_verbose)
                            HY1C_out("Input file %s is %s.\n", file->name, title);
                    } else {
                        fHY1C_out(stderr,
                                "-E- %s Line %d: Unknown sensor name in Level-2 file %s\n",
                                __FILE__, __LINE__, file->name);
                        return (fmt);
                    }
                } else {
                    fHY1C_out(stderr,
                            "-E- %s Line %d: No sensor name attribute in Level-2 file %s\n",
                            __FILE__, __LINE__, file->name);
                    return (fmt);
                }
            } else if (strcmp(title, "SeaWiFS Level-1A Data") == 0) {
                fmt = FMT_SEAWIFSL1A;
                file->format = fmt;
                file->sensorID = SEAWIFS;
                if (SDreadattr(sd_id, SDfindattr(sd_id, "Data Type"), (VOIDP) tempstr) == 0) {
                    if (strcmp(tempstr, "GAC") == 0) {
                        file->subsensorID = SEAWIFS_GAC;
                        if (want_verbose)
                            HY1C_out("Input file %s is SeaWiFS Level-1A GAC.\n", file->name);
                    } else if (strcmp(tempstr, "LAC") == 0) {
                        file->subsensorID = SEAWIFS_LAC;
                        if (want_verbose)
                            HY1C_out("Input file %s is SeaWiFS Level-1A LAC.\n", file->name);
                    } else {
                        file->subsensorID = SEAWIFS_LAC;
                        if (want_verbose)
                            HY1C_out("Input file %s is assumed to be SeaWiFS Level-1A LAC.\n", file->name);
                    }
                } else {
                    file->subsensorID = SEAWIFS_LAC;
                    if (want_verbose)
                        HY1C_out("Input file %s is assumed to be SeaWiFS Level-1A LAC.\n", file->name);
                }
            } else if (strcmp(title, "OCTS Level-1A GAC Data") == 0) {
                fmt = FMT_OCTSL1A;
                file->format = fmt;
                file->sensorID = OCTS;
                if (want_verbose)
                    HY1C_out("Input file %s is %s.\n", file->name, title);
            } else if (strcmp(title, "OSMI Level-1A Data") == 0) {
                fmt = FMT_OSMIL1A;
                file->format = fmt;
                file->sensorID = OSMI;
                if (want_verbose)
                    HY1C_out("Input file %s is %s.\n", file->name, title);
            } else if (strcmp(title, "CZCS Level-1A Data") == 0) {
                fmt = FMT_CZCSL1A;
                file->format = fmt;
                file->sensorID = CZCS;
                if (want_verbose)
                    HY1C_out("Input file %s is %s.\n", file->name, title);
            } else if (strcmp(title, "OCM1 Level-1B (OBPG)") == 0) {
                fmt = FMT_OCML1B;
                file->format = fmt;
                file->sensorID = OCM1;
                if (want_verbose)
                    HY1C_out("Input file %s is %s.\n", file->name, title);
            } else if (strncmp(title, "OCM Level-1B", 12) == 0) {
                fmt = FMT_OCML1BDB;
                file->format = fmt;
                file->sensorID = OCM1;
                if (want_verbose)
                    HY1C_out("Input file %s is %s.\n", file->name, title);
            } else if (strcmp(title, "Oceansat OCM2 Level-1B Data") == 0) {
                fmt = FMT_OCM2L1B;
                file->format = fmt;
                file->sensorID = OCM2;
                if (want_verbose)
                    HY1C_out("Input file %s is %s.\n", file->name, title);

                /* generic L1B format support */
            } else if (strcmp(title, "SeaWiFS Level-1B") == 0) {
                fmt = FMT_L1HDF;
                file->format = fmt;
                file->sensorID = SEAWIFS;
                if (want_verbose)
                    HY1C_out("Input file %s is %s.\n", file->name, title);
                
                 /* generic L1B MERIS format support */
             } else if (strcmp(title, "MERIS Level-1B") == 0) {
                fmt = FMT_L1HDF;
                file->format = fmt;
                file->sensorID = MERIS;
                if (want_verbose)
                    HY1C_out("Input file %s is %s.\n", file->name, title);
           } else if (strcmp(title, "OCM2 Level-1B") == 0) {
                fmt = FMT_L1HDF;
                file->format = fmt;
                file->sensorID = OCM2;
                if (want_verbose)
                    HY1C_out("Input file %s is %s.\n", file->name, title);
            } else if (strcmp(title, "OCTS Level-1B") == 0) {
                fmt = FMT_L1HDF;
                file->format = fmt;
                file->sensorID = OCTS;
                if (want_verbose)
                    HY1C_out("Input file %s is %s.\n", file->name, title);
            } else if (strcmp(title, "MOS Level-1B") == 0) {
                fmt = FMT_L1HDF;
                file->format = fmt;
                file->sensorID = MOS;
                if (want_verbose)
                    HY1C_out("Input file %s is %s.\n", file->name, title);
            } else if (strcmp(title, "OCTS Level-1B LAC Data") == 0) {
                fmt = FMT_OCTSL1B;
                file->format = fmt;
                file->sensorID = OCTS;
                if (want_verbose)
                    HY1C_out("Input file %s is %s.\n", file->name, title);
            } else if (strcmp(title, "OSMI Level-1B") == 0) {
                fmt = FMT_L1HDF;
                file->format = fmt;
                file->sensorID = OSMI;
                if (want_verbose)
                    HY1C_out("Input file %s is %s.\n", file->name, title);
            } else if (strcmp(title, "MODIST Level-1B") == 0) {
                fmt = FMT_L1HDF;
                file->format = fmt;
                file->sensorID = MODIST;
                if (want_verbose) {
                    HY1C_out("Input file %s is %s.\n", file->name, title);
                    HY1C_out("\n\n *** WARNING: polarization can not be computed from generic format ***\n\n");
                }
            } else if (strcmp(title, "MODISA Level-1B") == 0) {
                fmt = FMT_L1HDF;
                file->format = fmt;
                file->sensorID = MODISA;
                if (want_verbose) {
                    HY1C_out("Input file %s is %s.\n", file->name, title);
                    HY1C_out("\n\n *** WARNING: polarization can not be computed from generic format ***\n\n");
                }
            } else if (strcmp(title, "HMODIST Level-1B") == 0) {
                fmt = FMT_L1HDF;
                file->format = fmt;
                file->sensorID = HMODIST;
                if (want_verbose) {
                    HY1C_out("Input file %s is %s.\n", file->name, title);
                    HY1C_out("\n\n *** WARNING: polarization can not be computed from generic format ***\n\n");
                }
            } else if (strcmp(title, "HMODISA Level-1B") == 0) {
                fmt = FMT_L1HDF;
                file->format = fmt;
                file->sensorID = HMODISA;
                if (want_verbose) {
                    HY1C_out("Input file %s is %s.\n", file->name, title);
                    HY1C_out("\n\n *** WARNING: polarization can not be computed from generic format ***\n\n");
                }
            } else if (strcmp(title, "CZCS Level-1B") == 0) {
                fmt = FMT_L1HDF;
                file->format = fmt;
                file->sensorID = CZCS;
                if (want_verbose)
                    HY1C_out("Input file %s is %s.\n", file->name, title);

            } else if (strstr(title, "Level-1 cross-calibration pixels") != NULL) {
                if (SDreadattr(sd_id, SDfindattr(sd_id, "sensorID"), (VOIDP)&(file->sensorID)) != 0) {
                    fHY1C_out(stderr,
                            "-E- %s Line %d: Unrecognized sensor name, title %s in input HDF file %s\n",
                            __FILE__, __LINE__, title, file->name);
                    return (fmt);
                }
                fmt = FMT_L1XCAL;
                file->format = fmt;
                if (want_verbose)
                    HY1C_out("Input file %s is %s.\n", file->name, title);
            } else {
                fHY1C_out(stderr,
                        "-E- %s Line %d: Unrecognized title %s in input HDF file %s\n",
                        __FILE__, __LINE__, title, file->name);
                return (fmt);
            }

        } else if (SDreadattr(sd_id, SDfindattr(sd_id, "title"), (VOIDP) title) == 0) {

            if (strstr(title, "AVHRR") != NULL) {
                fmt = FMT_CLASSAVHRR;
                file->format = fmt;
                file->sensorID = AVHRR;
                if (want_verbose)
                    HY1C_out("Input file %s is %s.\n", file->name, title);
            }

        } else if (SDreadattr(sd_id, SDfindattr(sd_id, "satellite"), (VOIDP) title) == 0) {

            if (strstr(title, "oceansat-1") != NULL) {
                fmt = FMT_OCML1BDB;
                file->format = fmt;
                file->sensorID = OCM1;
                if (want_verbose)
                    HY1C_out("Input file %s is %s.\n", file->name, "OCM1 DB file");
            }

            /* Is it HDF-EOS L1B format? */

        } else if (SDreadattr(sd_id, SDfindattr(sd_id, "ArchiveMetadata.0"), (VOIDP) eosmeta) == 0) {
            if (SDnametoindex(sd_id, "EV_250_Aggr1km_RefSB") != -1) {
                if (getHDFattr(sd_id, "Rescaled Ocean R", "EV_250_Aggr1km_RefSB", (VOIDP) tempstr) == 0) {
                    if (want_verbose)
                        HY1C_out("File contains only the ocean band subset.\n");
                    ocean_subset = 1;
                }
            }
            if (file->modis_subset_compat == 1) {
                ocean_subset = 1;
            }

            if (strstr(eosmeta, "MODIS/Terra Calibrated Radiances 5-Min L1B Swath 1km") != NULL) {
                if (ocean_subset == 1) {
                    fmt = FMT_MODISL1B;
                    file->format = fmt;
                    file->sensorID = MODIST;
                } else {
                    fmt = FMT_HMODISL1B;
                    file->format = fmt;
                    file->sensorID = HMODIST;
                }
                if (want_verbose)
                    HY1C_out("Input file %s is MODIS Terra Level-1B HDF-EOS product.\n",
                        file->name);
            } else if (strstr(eosmeta, "MODIS/Aqua Calibrated Radiances 5-Min L1B Swath 1km") != NULL) {
                if (ocean_subset == 1) {
                    fmt = FMT_MODISL1B;
                    file->format = fmt;
                    file->sensorID = MODISA;
                } else {
                    fmt = FMT_HMODISL1B;
                    file->format = fmt;
                    file->sensorID = HMODISA;
                }
                if (want_verbose)
                    HY1C_out("Input file %s is MODIS Aqua Level-1B HDF-EOS product.\n",
                        file->name);
            } else if (strstr(eosmeta, "MODIS/Aqua Calibrated Radiances 5-Min L1B Swath 250m") != NULL) {
                HY1C_out("Input file %s is MODIS Aqua Level-1B HDF-EOS product at 250m Resolution.\nThis file is no longer accepted as input.\nPlease use the 1KM (LAC) file and set resolution=250\n",
                        file->name);
                return (fmt);
            } else if (strstr(eosmeta, "MODIS/Aqua Calibrated Radiances 5-Min L1B Swath 500m") != NULL) {
                HY1C_out("Input file %s is MODIS Aqua Level-1B HDF-EOS product at 500m Resolution.\nThis file is no longer accepted as input.\nPlease use the 1KM (LAC) file and set resolution=500\n",
                        file->name);
                return (fmt);
            } else if (strstr(eosmeta, "MODIS/Terra Calibrated Radiances 5-Min L1B Swath 250m") != NULL) {
                HY1C_out("Input file %s is MODIS Terra Level-1B HDF-EOS product at 250m Resolution.\nThis file is no longer accepted as input.\nPlease use the 1KM (LAC) file and set resolution=250\n",
                        file->name);
                return (fmt);
            } else if (strstr(eosmeta, "MODIS/Terra Calibrated Radiances 5-Min L1B Swath 500m") != NULL) {
                HY1C_out("Input file %s is MODIS Terra Level-1B HDF-EOS product at 500m Resolution.\nThis file is no longer accepted as input.\nPlease use the 1KM (LAC) file and set resolution=500\n",
                        file->name);
                return (fmt);
            } else if (strstr(eosmeta, "MODIS/Aqua Geolocation Fields") != NULL) {
                fmt = FMT_MODISGEO;
                file->format = fmt;
                file->sensorID = MODISA;
                if (want_verbose)
                    HY1C_out("Input file %s is MODIS Aqua Geolocation Fields.\n",
                        file->name);
            } else if (strstr(eosmeta, "MODIS/Terra Geolocation Fields") != NULL) {
                fmt = FMT_MODISGEO;
                file->format = fmt;
                ;
                file->sensorID = MODIST;
                if (want_verbose)
                    HY1C_out("Input file %s is MODIS Terra Geolocation Fields.\n",
                        file->name);
            } else {
                fHY1C_out(stderr,
                        "-E- %s Line %d: Unrecognized HDF-EOS file %s\n",
                        __FILE__, __LINE__, file->name);
                return (fmt);
            }

            /* Is it MODIS IMAPP Direct Broadcast L1B */

        } else if (SDreadattr(sd_id, SDfindattr(sd_id, "ASSOCIATEDPLATFORMSHORTNAME"), (VOIDP) eosmeta) == 0) {

            if (strstr(eosmeta, "Aqua") != NULL) {
                fmt = FMT_MODISL1B;
                file->format = fmt;
                file->sensorID = MODISA;
                if (want_verbose)
                    HY1C_out("Input file %s is MODIS Aqua Level-1B IMAPP product.\n",
                        file->name);
            } else if (strstr(eosmeta, "Terra") != NULL) {
                fmt = FMT_MODISL1B;
                file->format = fmt;
                file->sensorID = MODIST;
                if (want_verbose)
                    HY1C_out("Input file %s is MODIS Terra Level-1B IMAPP product.\n",
                        file->name);
            } else {
                fHY1C_out(stderr,
                        "-E- %s Line %d: Unrecognized IMAPP format %s\n",
                        __FILE__, __LINE__, file->name);
                return (fmt);
            }


            /* Is it MOS L1B HDF standard product? */

        } else if (GetFileDesc(file->name) != NULL) {

            fmt = FMT_MOSL1B;
            file->format = fmt;
            file->sensorID = MOS;
            if (want_verbose)
                HY1C_out("Input file %s is MOS Level-1B standard product.\n",
                    file->name);

        } else {

            fHY1C_out(stderr, "-E- %s Line %d: Unrecognized input HDF file %s\n",
                    __FILE__, __LINE__, file->name);
            return (fmt);
        }

        SDend(sd_id);

        /*  is the file in HDF 5 format (and thus a VIIRS file)?
            (check for a list of viirs M bands later)   */
    } else if ((fmt = chk_viirs(file->name, file)) != -1) {

        if (want_verbose)
            HY1C_out("%s, %d: HDF 5 file\n", __FILE__, __LINE__);
    } else {

        FILE *fp;
        char vfname[500];

        /* Open file */
        if ((fp = fopen(file->name, "r")) == NULL) {
            fHY1C_out(stderr, "-E- : input file %s does not exist or is read protected.\n",
                    file->name);
            return (fmt);
        }

        /* see if we have list of VIIRS files */
        if (fscanf(fp, "%s", vfname) == 1) {
            if ((fmt = chk_viirs(vfname, file)) != -1) {
                if (file->format == FMT_VIIRSGEO) {
                    fHY1C_out(stderr,
                            "-E- : File list cannot be used for a VIIRS GEO file\n");
                    return (-1);
                }
                fclose(fp);
                return (fmt);
            }
        }

        fclose(fp);

        /* Is it MERIS? */
        {
            EPR_SProductId *product_id;

            epr_init_api(e_log_debug, NULL, NULL);
            product_id = epr_open_product(file->name);
            if (product_id != NULL) {
                if (product_id->id_string[8] == '1') { /* it is a level 1 file */
                    fmt = FMT_MERISL1B;
                    file->format = fmt;
                    file->sensorID = MERIS;
                    if (want_verbose)
                        HY1C_out("Input file %s is MERIS L1 file.\n",
                            file->name);
                } else if (product_id->id_string[8] == '2') { /* it is a level 2 file */
                    fmt = FMT_MERISL2;
                    file->format = fmt;
                    file->sensorID = MERISL2;
                    if (want_verbose)
                        HY1C_out("Input file %s is MERIS L2 file.\n",
                            file->name);
                }
                epr_close_product(product_id);
                /*remember to close api (epr_close_api();)*/
                return (fmt);
            } else {
                // Check for MERIS CC
                status = nc_open(file->name, NC_NOWRITE, &ncid);
                if (status == NC_NOERR) {
                    size_t length;
                    status = nc_inq_attlen(ncid, NC_GLOBAL, "title", &length);
                    if (status == NC_NOERR) {
                        char *titleStr = (char*) malloc(length + 1);
                        if (titleStr) {
                            status = nc_get_att_text(ncid, NC_GLOBAL, "title", titleStr);
                            if (status == NC_NOERR) {
                                titleStr[length] = '\0';
                                lowcase(titleStr);
                                if (strstr(titleStr, "meris l1b")) {
                                    fmt = FMT_MERISCC;
                                    file->format = fmt;
                                    file->sensorID = MERIS;
                                    if (want_verbose)
                                        HY1C_out("Input file %s is MERIS CC file.\n", file->name);
                                } // found it
                            } // read title global attribute
                            free(titleStr);
                        } // allocated memory
                    } // got title length
                    nc_close(ncid);
                } // opened netCDF file

            } // not MERIS N1 file
        }
    }

    return (fmt);
}

int32_t chk_viirs(char *fname, filehandle *file)
/* ------------------------------------------------------------------------
   chk_viirs

   purpose: check a file to see if it is a VIIRS file

   Returns -1 if not hdf5 and VIIRS or the format code

   Parameters: (in calling order)
      Type              Name            I/O     Description
      ----              ----            ---     -----------
      char *            fname            I      file to check
      filehandle *      file             I      input file information

   Modification history:
      Programmer        Date            Description of change
      ----------        ----            ---------------------
      W. Robinson, SAIC,  2 Aug, 2010   Original development

 ------------------------------------------------------------------------*/ {
    int32_t fmt = -1;
    char inst_name[200], miss_name[200], geoloc_ref[200], **grp_obj_nm;
    h5io_str h5fid, g_id;
    int is_viirs = 1, *grp_obj_typ, n_obj;
    if (h5io_openr(fname, 0, &h5fid) == 0) {
        /*
         *  If the Instrument_Short_Name is in the top, make sure it is VIIRS
         */
        if (h5io_attr_exist(&h5fid, "Instrument_Short_Name") == 0) {
            if (h5io_rd_attr(&h5fid, "Instrument_Short_Name", (void *) inst_name)
                    != 0) is_viirs = 0;
            if (strcmp(inst_name, "VIIRS") != 0) is_viirs = 0;
        }
        if ((is_viirs == 1) &&
                (h5io_rd_attr(&h5fid, "Mission_Name", (void *) miss_name) != 0))
            is_viirs = 0;
        /*
         *  To accomodate the observed (in datasets) and possible (in CDFCB
         *  metadata V5 doc) mission names, look for either NPP or NPOESS
         *  at the start of the string
         */
        if ((is_viirs == 1) &&
                ((strncmp(miss_name, "NPP", 3) != 0) &&
                (strncmp(miss_name, "NPOESS", 6) != 0))) is_viirs = 0;
        /* it is a VIIRS file - only need to know if its band or geo */
        /*
         *  set to the All_Data group and look for a geo or rad group
         */
        if (is_viirs &&
                (h5io_set_grp(&h5fid, "All_Data", &g_id) != 0)) is_viirs = 0;

        if (is_viirs &&
                (h5io_grp_contents(&g_id, &n_obj, &grp_obj_nm, &grp_obj_typ)
                != 0)) is_viirs = 0;
        if (is_viirs) {
            /*
             *  This is set up only for un-bundled bands / geo - if otherwise, note it
             */
            if (n_obj != 1) {
                HY1C_out("%s, %d: Warning: the file:\n%s\nIs bundled\n", __FILE__,
                        __LINE__, fname);
                HY1C_out("\twith %d items\n", n_obj);
            }
            if ((strcmp(grp_obj_nm[0], "VIIRS-MOD-GEO_All") == 0) ||
                    (strcmp(grp_obj_nm[0], "VIIRS-MOD-GEO-TC_All") == 0)) {
                fmt = FMT_VIIRSGEO;
                file->format = fmt;
                file->sensorID = VIIRS;
                if (want_verbose)
                    HY1C_out("Input file %s is VIIRS GEO(LOCATION) file.\n",
                        fname);
            } else if ((strcmp(grp_obj_nm[0], "VIIRS-M1-SDR_All") == 0) ||
                    (strcmp(grp_obj_nm[0], "VIIRS-M2-SDR_All") == 0) ||
                    (strcmp(grp_obj_nm[0], "VIIRS-M3-SDR_All") == 0) ||
                    (strcmp(grp_obj_nm[0], "VIIRS-M4-SDR_All") == 0) ||
                    (strcmp(grp_obj_nm[0], "VIIRS-M5-SDR_All") == 0) ||
                    (strcmp(grp_obj_nm[0], "VIIRS-M6-SDR_All") == 0) ||
                    (strcmp(grp_obj_nm[0], "VIIRS-M7-SDR_All") == 0) ||
                    (strcmp(grp_obj_nm[0], "VIIRS-M8-SDR_All") == 0) ||
                    (strcmp(grp_obj_nm[0], "VIIRS-M9-SDR_All") == 0) ||
                    (strcmp(grp_obj_nm[0], "VIIRS-M10-SDR_All") == 0) ||
                    (strcmp(grp_obj_nm[0], "VIIRS-M11-SDR_All") == 0) ||
                    (strcmp(grp_obj_nm[0], "VIIRS-M12-SDR_All") == 0) ||
                    (strcmp(grp_obj_nm[0], "VIIRS-M13-SDR_All") == 0) ||
                    (strcmp(grp_obj_nm[0], "VIIRS-M14-SDR_All") == 0) ||
                    (strcmp(grp_obj_nm[0], "VIIRS-M15-SDR_All") == 0) ||
                    (strcmp(grp_obj_nm[0], "VIIRS-M16-SDR_All") == 0)) {
                fmt = FMT_VIIRSL1B;
                file->format = fmt;
                file->sensorID = VIIRS;
                if (want_verbose)
                    HY1C_out("Input file %s is VIIRS Level-1B standard product.\n",
                        fname);
            } else
                is_viirs = 0;
        }
        if (is_viirs == 0) {
            fHY1C_out(stderr, "-E- %s Line %d: Unrecognized input HDF5 file %s\n",
                    __FILE__, __LINE__, fname);
        }
        h5io_close(&h5fid);
    }
    return (fmt);
}
